package com.woniu.userservice.controller;

import com.woniu.userservice.entity.Biddings;
import com.woniu.userservice.entity.ResponseResult;
import com.woniu.userservice.entity.ResultBody;
import com.woniu.userservice.entity.ReturnBody;
import com.woniu.userservice.service.BiddingsService;
import com.woniu.userservice.util.FtpUtil;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * 出价表   包含用户的出价信息（还未开始编写） ；包含用户的模糊搜索出价信息 展示信息 删除信息
 *
 * 还要将用户参与竞拍的方法书写到这个里面
 */
@RestController
@RequestMapping("/biddings")
public class BiddingsController {
    @Resource
    private BiddingsService biddingsService;


    @GetMapping("selectOne")
    public Biddings selectOne(Integer id) {
        return biddingsService.queryById(id);
    }

    /**
     *
     * @param file
     * @param req
     * @return 图片上传 接口
     * @throws IllegalStateException
     * @throws Exception
     */
    @PostMapping("/fileUpload")
    @ResponseBody
    public ResponseResult moomaUpload(@RequestParam("waremooma") MultipartFile file, HttpServletRequest req) throws IllegalStateException, Exception{
        String fileURL = FtpUtil.upload(file.getInputStream(), file.getOriginalFilename());
        return new ResponseResult<String>(200, 10, "ok", fileURL);
    }

    /**
     *
     * @param id
     * @param userId
     * @return 删除用户的出价记录
     */
    @PostMapping("/deleteBiddings")
    public String deleteBiddings(@RequestParam(value = "id",required = true) Integer id,@RequestParam(value = "userId",required = true) Integer userId){

        String state = null;

        Boolean bo = biddingsService.deleteByUserId(id,userId);
        if (bo){
            state = "删除成功";
            return state;
        }else {
            state = "删除失败";
        return state;
        }
    }

    /**
     *
     * @param goodsName
     * @param userId
     * @param price
     * @param biddingTime
     * @return 模糊查询出价记录 搜索功能
     */
    @PostMapping("/findByMsg")
    @ResponseBody
    public List<Biddings> findByMsg(@RequestParam(required = true) String goodsName, @RequestParam(value = "userId",required = true) Integer userId, @RequestParam(required = true) Double price, Date biddingTime){

        List<Biddings> biddings = biddingsService.findByMsg(goodsName,userId,price,biddingTime);
        if (biddings.size()==0){
            return null;
        }else {
        return biddings;
        }
    }
    /**
     * 出价记录显示；
     */
    @GetMapping("/findAllMsg/{userId}")
    public List<Biddings> findAllMsg(@PathVariable("userId") Integer userId){
        return null;
    }

    /**
     * 以下内容是我的竞拍接口---------------------------------------------------------------------
     *        包含 删除竞拍信息   取消竞拍信息  商品查看信息   商品信息预加载（需要跨微服务获取）
     */
    @GetMapping("/deleteMyMsg")
    public String deleteMyMsg(Integer goodId ,Integer userId){


        return "删除失败";
    }

    /**
     * Description : TODO  获取竞拍最后成交价格<br/>
     * ChangeLog : 1. 创建 (2020/3/27 9:25 [李浪]);
     * @return null
    */
    @PostMapping("/findByGoodsId")
    @ResponseBody
    public ResultBody findByGoodsId(@RequestParam(value = "userId",required = true) Integer userId){
//
//        List<Biddings> biddings = biddingsService.findByMsg(goodsName,userId,price,biddingTime);
//        if (biddings.size()==0){
//            return null;
//        }else {
//            return biddings;
//        }
        return  ResultBody.success();
    }
 }